import { defineStore } from 'pinia'
import { ref } from 'vue'
import request from '@/utils/request'

export const useNoticeStore = defineStore('notice', () => {
    const unreadCount = ref(0)
    const list        = ref([])

    // 未读数量
    const getUnreadCount = async () => {
        const res = await request.get('/notice/unread-count')
        unreadCount.value = res.data ?? 0
    }

    // 列表
    const getList = async (page = 1, size = 10) => {
        const res = await request.get('/notice/list', { page, size })
        list.value = res.data ?? []
    }

    // 单条已读
    const readOne = async (id) => {
        await request.put(`/notice/read/${id}`)
        await getUnreadCount()
    }

    // 全部已读
    const readAll = async () => {
        await request.put('/notice/read-all')
        await getUnreadCount()
    }

    return { unreadCount, list, getUnreadCount, getList, readOne, readAll }
})